<template>
  <div>
    <!-- 面包屑区域 组件化传值 -->
    <Header :titleName = "titleObj" ></Header>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索框和添加按钮 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoodsPages">增加列表</el-button>
        </el-col>
      </el-row>
      <!-- 表单区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
          width="340px"
        ></el-table-column>
        <el-table-column label="价格" prop="goods_price"></el-table-column>
        <el-table-column label="数量" prop="goods_number"></el-table-column>
        <el-table-column label="重量" prop="goods_weight"></el-table-column>
        <el-table-column label="商品状态">
          <!-- 插槽的用处 -->
          <template slot-scope="scope">
            {{
              scope.row.goods_state === 0
                ? '未通过'
                : scope.row.goods_state === 1
                ? '审核中'
                : '已审核'
            }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" width="160px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="更新时间" prop="upd_time">
             <template slot-scope="scope">
                {{scope.row.upd_time | dataFormat}}
            </template>
        </el-table-column> -->
        <el-table-column label="热销品数量" prop="hot_mumber"></el-table-column>
        <el-table-column label="是否热销品" prop="is_promote">
          <template slot-scope="scope">
            {{ scope.row.is_promote ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editClick(scope.row)"
              >编辑</el-button
            >
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import Header from '../shard/header.vue'
export default {
  data() {
    return {
       titleObj:{ firstName:'商品管理',scendName:'商品列表'},
      total: 0,
      inputValue: '',
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      goodsList: [],
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('/goods', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败！')
      }
      this.$message.success('获取商品列表成功')
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },
    editClick(row) {
      this.$router.push({ name:'goodsEdit',params:{
        goodsObj:row
      }})
    },
    async deleteGoods(id) {
      let resultConfirm = await this.$confirm(
        '此操作将永久删除该分类, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      if (resultConfirm != 'confirm') {
        return this.$message.warning('取消删除')
      }
      let { data: res } = await this.$http.delete('/goods/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败！')
      }
      this.$message.success('删除该商品成功！')
      this.getGoodsList()
    },
    addGoodsPages() {
      this.$router.push('/goods/add')
    },
  },
  components:{
    Header
  }
}
</script>

<style lang="less" scoped>
</style>